<?php
/**
 * 系统设置页面
 * 相对路径: adm/site.php
 * 作者: 15058593138@qq.com
 */

// 保存设置
if (isset($_GET['act']) && $_GET['act'] === 'save') {
    $autoRefresh = isset($_POST['auto_refresh']) ? intval($_POST['auto_refresh']) : 15;
    $pageSize = isset($_POST['page_size']) ? intval($_POST['page_size']) : 20;
    $yuTian = isset($_POST['yu_tian']) ? intval($_POST['yu_tian']) : 7;
    
    if ($autoRefresh < 5 || $autoRefresh > 300) {
        jsonMsg(0, '自动刷新间隔应在5-300秒之间');
    }
    
    if ($pageSize < 10 || $pageSize > 100) {
        jsonMsg(0, '分页数量应在10-100之间');
    }
    
    if ($yuTian < 1 || $yuTian > 30) {
        jsonMsg(0, '预约天数应在1-30天之间');
    }
    
    // 读取配置文件
    $configFile = './inc/conn.php';
    $content = file_get_contents($configFile);
    
    // 更新配置
    $content = preg_replace("/define\('AUTO_REFRESH',\s*\d+\);/", "define('AUTO_REFRESH', {$autoRefresh});", $content);
    $content = preg_replace("/define\('PAGE_SIZE',\s*\d+\);/", "define('PAGE_SIZE', {$pageSize});", $content);
    $content = preg_replace("/define\('YU_TIAN',\s*\d+\);/", "define('YU_TIAN', {$yuTian});", $content);
    
    if (file_put_contents($configFile, $content)) {
        jsonMsg(1, '设置保存成功');
    } else {
        jsonMsg(0, '设置保存失败，请检查文件权限');
    }
}

require_once './inc/head.php';
?>

<div class="container">
    <div class="table-wrapper">
        <div style="padding:30px;">
            <h3>系统设置</h3>
            
            <form id="settingForm" style="margin-top:30px; max-width:600px;">
                <div class="form-group">
                    <label>自动刷新间隔（秒）</label>
                    <input type="number" name="auto_refresh" class="form-control" 
                           value="<?php echo AUTO_REFRESH; ?>" min="5" max="300" required>
                    <small style="color:#666;">座位表自动刷新的时间间隔，建议10-30秒</small>
                </div>
                
                <div class="form-group">
                    <label>分页数量（条/页）</label>
                    <input type="number" name="page_size" class="form-control" 
                           value="<?php echo PAGE_SIZE; ?>" min="10" max="100" required>
                    <small style="color:#666;">列表每页显示的记录数量</small>
                </div>
                
                <div class="form-group">
                    <label>预约提前天数</label>
                    <input type="number" name="yu_tian" class="form-control" 
                           value="<?php echo YU_TIAN; ?>" min="1" max="30" required>
                    <small style="color:#666;">用户可以提前多少天预约座位</small>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">保存设置</button>
                    <button type="reset" class="btn btn-secondary">重置</button>
                </div>
                
                <div style="margin-top:30px; padding:15px; background:#f8f9fa; border-radius:5px;">
                    <h4 style="margin-bottom:10px;">系统信息</h4>
                    <table style="width:100%; font-size:14px;">
                        <tr>
                            <td style="padding:5px 0; width:150px;">系统版本：</td>
                            <td><?php echo VERSION; ?></td>
                        </tr>
                        <tr>
                            <td style="padding:5px 0;">PHP版本：</td>
                            <td><?php echo PHP_VERSION; ?></td>
                        </tr>
                        <tr>
                            <td style="padding:5px 0;">数据库：</td>
                            <td><?php echo DB_NAME; ?></td>
                        </tr>
                        <tr>
                            <td style="padding:5px 0;">服务器时间：</td>
                            <td><?php echo date('Y-m-d H:i:s'); ?></td>
                        </tr>
                    </table>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.getElementById('settingForm').onsubmit = function(e) {
        e.preventDefault();
        
        const formData = getFormData('settingForm');
        
        showLoading();
        ajax('admin.php?do=site&act=save', formData, function(res) {
            hideLoading();
            if (res.code === 1) {
                toast(res.msg, 'success');
                setTimeout(() => {
                    location.reload();
                }, 1500);
            } else {
                toast(res.msg, 'error');
            }
        });
    };
</script>

<?php require_once './inc/foot.php'; ?>